<?php
/**
 * Created by PhpStorm.
 * User: sosyuki
 * Date: 2018/3/29
 * Time: 16:06
 */

namespace Drupal\easy_wechat;


use Psr\SimpleCache\CacheInterface;

class EasyWechatDrupalCache implements CacheInterface {

  public function get($key, $default = NULL) {
    $cache = cache_get($key, 'cache_easy_wechat');
    if (!empty($cache)) {
      return $cache->data;
    }
    return $default;
  }

  public function delete($key) {
    cache_clear_all($key, 'cache_easy_wechat');
    return TRUE;
  }

  public function clear() {
    cache_clear_all(NULL, 'cache_easy_wechat');
    return TRUE;
  }

  public function getMultiple($keys, $default = NULL) {
    $caches = cache_get_multiple($cids, 'cache_easy_wechat');
    $list = [];
    foreach ($keys as $key) {
      if (isset($caches[$key])) {
        $cache = $caches[$key];
        $list[$key] = $cache->data;
      }
      else {
        $list[$key] = $default;
      }
    }
    return $list;
  }

  public function setMultiple($values, $ttl = NULL) {
    foreach ($values as $key => $value) {
      $this->set($key, $value, $ttl);
    }
    return TRUE;
  }

  public function set($key, $value, $ttl = NULL) {
    //  watchdog('__debug__', serialize($ttl));
    cache_set($key, $value, 'cache_easy_wechat', REQUEST_TIME + $ttl);
    return TRUE;
  }

  public function deleteMultiple($keys) {
    cache_clear_all($keys, 'cache_easy_wechat');
    return TRUE;
  }

  public function has($key) {
    $cache = cache_get($key, 'cache_easy_wechat');
    if (!empty($cache) && $cache->expire > REQUEST_TIME) {
      return TRUE;
    }
    return FALSE;
  }

}
